using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using Campus.Entities;

namespace CampusSite.Models
{ 
    public class ObraSocialRepository : IObraSocialRepository
    {
        CampusSiteContext context = new CampusSiteContext();

        public IQueryable<ObraSocial> All
        {
            get { return context.ObrasSociales; }
        }

        public IQueryable<ObraSocial> AllIncluding(params Expression<Func<ObraSocial, object>>[] includeProperties)
        {
            IQueryable<ObraSocial> query = context.ObrasSociales;
            foreach (var includeProperty in includeProperties) {
                query = query.Include(includeProperty);
            }
            return query;
        }

        public ObraSocial Find(int id)
        {
            return context.ObrasSociales.Find(id);
        }

        public void InsertOrUpdate(ObraSocial obrasocial)
        {
            if (obrasocial.ObraSocialId == default(int)) {
                // New entity
                context.ObrasSociales.Add(obrasocial);
            } else {
                // Existing entity
                context.Entry(obrasocial).State = EntityState.Modified;
            }
        }

        public void Delete(int id)
        {
            var obrasocial = context.ObrasSociales.Find(id);
            context.ObrasSociales.Remove(obrasocial);
        }

        public void Save()
        {
            context.SaveChanges();
        }

        public void Dispose() 
        {
            context.Dispose();
        }
    }

    public interface IObraSocialRepository : IDisposable
    {
        IQueryable<ObraSocial> All { get; }
        IQueryable<ObraSocial> AllIncluding(params Expression<Func<ObraSocial, object>>[] includeProperties);
        ObraSocial Find(int id);
        void InsertOrUpdate(ObraSocial obrasocial);
        void Delete(int id);
        void Save();
    }
}